//============================================================
// Producnt name:		Wz
// Version: 			1.0
// Coded by:			Wz (13066628480@163.com)
// Auto generated at: 	2016/10/13 11:33:09
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using Cloud.Model;
using System.Data.SqlClient;
using System.Data;
namespace Cloud.DAL
{
	public static class NoticesService
	{
		#region SqlCommandText
		private const string Save_CommandText = "insert into Notices values(@title,@rtime,@cons,@files,@relname)";
		private const string Update_CommandText = "Update Notices set title = @title,rtime = @rtime,cons = @cons,files = @files,relname = @relname where id = @id";
		private const string Delete_CommandText = "delete from Notices where id = @id";
		private const string FindAll_CommandText = "select * from Notices order by id desc";
		private const string FindById_CommandText = "select * from Notices where id=@id";
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion
		
		#region Save
		public static int Save(Notices notices)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@title",SqlDbType.VarChar),
				new SqlParameter("@rtime",SqlDbType.DateTime),
				new SqlParameter("@cons",SqlDbType.Text),
				new SqlParameter("@files",SqlDbType.VarChar),
				new SqlParameter("@relname",SqlDbType.VarChar)
				};
			pars[0].Value=notices.Title;
			pars[1].Value=notices.Rtime;
			pars[2].Value=notices.Cons;
			pars[3].Value=notices.Files;
			pars[4].Value=notices.Relname;
	
			return DBHelper.ExecuteNonQuery(Save_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region Update
		public static int Update(Notices notices)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int),
				new SqlParameter("@title",SqlDbType.VarChar),
				new SqlParameter("@rtime",SqlDbType.DateTime),
				new SqlParameter("@cons",SqlDbType.Text),
				new SqlParameter("@files",SqlDbType.VarChar),
				new SqlParameter("@relname",SqlDbType.VarChar)
				};
			pars[0].Value=notices.Id;
			pars[1].Value=notices.Title;
			pars[2].Value=notices.Rtime;
			pars[3].Value=notices.Cons;
			pars[4].Value=notices.Files;
			pars[5].Value=notices.Relname;
	
			return DBHelper.ExecuteNonQuery(Update_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region Delete
		public static int Delete(Notices notices)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
				};
			pars[0].Value=notices.Id;
	
			return DBHelper.ExecuteNonQuery(Delete_CommandText,CommandType.Text,pars);
		}
		#endregion
		
		#region FindAll
		public static IList<Notices> FindAll()
		{
			return CommonQuery(FindAll_CommandText,CommandType.Text,null);
		}
		#endregion
		
		#region FindById
		public static Notices FindById(Int32 id)
		{
			SqlParameter[] pars = new SqlParameter[]{
				new SqlParameter("@id",SqlDbType.Int)
			};	
			pars[0].Value = id;
			IList<Notices> list = CommonQuery(FindById_CommandText,CommandType.Text,pars);
			return list.Count > 0 ? list[0] : null;
		}
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion
		
		#region CommonQuery
		private static IList<Notices> CommonQuery(string commandText, CommandType cmdType, SqlParameter[] pars)
		{
			IList<Notices> list = new List<Notices>();
			Notices notices = null;
			
			using (SqlDataReader reader = DBHelper.ExecuteQuery(commandText,cmdType,pars))
           	{
				while(reader.Read())
				{
					notices = new Notices();
					notices.Id = Convert.ToInt32(reader["id"]);
					notices.Title = Convert.ToString(reader["title"]);
					notices.Rtime = Convert.ToDateTime(reader["rtime"]);
					notices.Cons = Convert.ToString(reader["cons"]);
					notices.Files = Convert.ToString(reader["files"]);
					notices.Relname = Convert.ToString(reader["relname"]);
					
					list.Add(notices);
				}
            }
            return list;
		}
		#endregion
		
		#region ??
		
		#endregion
		
		#region ??
		
		#endregion

        #region Top5
        public static IList<Notices> Top5()
        {
            return CommonQuery("select top 5 * from notices order by id desc", CommandType.Text, null);
        } 
        #endregion
	}
}



